What is base64-stream?
The base64-stream npm package provides a streaming interface for encoding and decoding data in base64 format. It is particularly useful for handling large amounts of data that need to be processed in chunks, such as files or network streams.
What are base64-stream's main functionalities?
Base64 Encoding
This feature allows you to encode a stream of data into base64 format. In this example, the contents of 'input.txt' are read, encoded in base64, and then written to 'output.txt'.
const fs = require('fs');
const base64 = require('base64-stream');
fs.createReadStream('input.txt')
.pipe(base64.encode())
.pipe(fs.createWriteStream('output.txt'));
Base64 Decoding
This feature allows you to decode a base64-encoded stream of data. In this example, the contents of 'input.txt' are read, decoded from base64, and then written to 'output.txt'.
const fs = require('fs');
const base64 = require('base64-stream');
fs.createReadStream('input.txt')
.pipe(base64.decode())
.pipe(fs.createWriteStream('output.txt'));
Other packages similar to base64-stream
base64-js
The base64-js package provides utilities for encoding and decoding base64 strings. Unlike base64-stream, it does not offer a streaming interface and is more suited for smaller data sizes that can be handled in memory.
base64url
The base64url package is designed for encoding and decoding base64 URL-safe strings. It is useful for web applications where URL-safe encoding is required. However, it does not provide a streaming interface like base64-stream.
node-base64-image
The node-base64-image package allows you to encode and decode images to and from base64 strings. It is specialized for image data and does not offer a general-purpose streaming interface like base64-stream.
Introduction
While Node.js has built-in support for Base64 data, it does not come with the ability to encode / decode data in a stream.
This library contains a streaming Base64 encoder and a streaming Base64 decoder for use with Node.js. These classes are written using the Node.js stream interfaces and are well covered with unit tests.
Usage
Installation
To install base64-stream
npm install base64-stream
Examples
This example encodes an image and pipes it to stdout.
var http = require('http');
var {Base64Encode} = require('base64-stream');
var img = 'http://farm3.staticflickr.com/2433/3973241798_86ddfa642b_o.jpg';
http.get(img, function(res) {
if (res.statusCode === 200)
res.pipe(new Base64Encode()).pipe(process.stdout);
});
This example takes in Base64 encoded data on stdin, decodes it, an pipes it to stdout.
var {Base64Encode} = require('base64-stream');
process.stdin.pipe(new Base64Encode()).pipe(process.stdout);
options:
Base64Encode
can take an optional object {lineLength: number, prefix: string}
The prefix is useful for prepending for example data:image/png;base64,
to make a base64 URL.
This example proxies an image url, and send the base64 string in response.
app.get('/i/*', function(req, res){ // using express for example
fetch(req.params[0]) // using node-fetch
.then(r=>r.body.pipe(new Base64Encode({prefix:`data:${r.headers.get('content-type')};base64,`})).pipe(res))
.catch(console.error);
});
Requirements
This module currently requires Node 6.0.0 or higher.
Testing
To run the unit tests
npm test
License
MIT